<template>
	<view>
		<!-- 去搜索 -->
		<view class="search" @click="toSearch">
			请输入关键字搜索
			<image src="../../static/images/iconImg/sousuo.png" mode=""></image>
		</view>

		<!-- 轮播图 -->
		<view class="swiperOuter">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
				<swiper-item v-for="(item,index) in imgSlide" :key="index">
					<view class="swiper-item">
						<image :src="'http://192.144.206.29'+item.image" mode=""></image>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<!-- 导航按钮 -->
		<ul class="navigationBtn">
			<li v-for="(item,index) in navBar" :key="index">
				<image :src="'http://192.144.206.29'+item.image" mode=""></image>
				<view>{{item.text}}</view>
			</li>

		</ul>

		<!-- 热门商品 -->
		<view class="hotShop">
			<ul class="hotShop_top">
				<li>热门商品</li>
				<li @click="lookMore">查看更多 ></li>
			</ul>
			<ul class="hotCard">
				<li v-for="item in hotGoods" :key="item.id" @click="toDetail(item.id)">
					<image :src="item.image_url" mode=""></image>
					<p>{{item.name}}</p>
					<p>￥<span>{{item.price}}</span></p>
				</li>
			</ul>
		</view>

		<view class="swiperOuter">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
				<swiper-item v-for="(item,index) in imgSlide" :key="index">
					<view class="swiper-item">
						<image :src="'http://192.144.206.29'+item.image" mode=""></image>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<view class="hotShop">
			<ul class="hotShop_top">
				<li>推荐商品</li>
				<li @click="lookMore">查看更多 ></li>
			</ul>
			<ul class="hotCard">
				<li v-for="item in hotGoods" :key="item.id" @click="toDetail(item.id)">
					<image :src="item.image_url" mode=""></image>
					<p>{{item.name}}</p>
					<p>￥<span>{{item.price}}</span></p>
				</li>
			</ul>
		</view>

		<image class="footerImg" :src="imgUrl" mode=""></image>

	</view>
</template>

<script>
	import {
		getHomeDataApi
	} from '../../api/paiModule/home.js'
	export default {
		data() {
			return {
				imgUrl: 'http://192.144.206.29/static/images/empty-banner.png',
				token: '',
				imgSlide: [], // 轮播图图片
				navBar: [], // 导航按钮
				hotGoods: [], // 热门商品
			};
		},
		onShow() {
			this.getHomeData()
			this.yzToken()
		},
		created() {
			const token = uni.getStorageSync('token')
			this.token = token
		},
		methods: {
			// 去搜索页面
			toSearch() {
				uni.navigateTo({
					url: '../searchPage/searchPage'
				})
			},

			// 验证token效果
			async yzToken() {
				const {
					data: res
				} = await getHomeDataApi({
					method: "cart.getnumber",
					token: uni.getStorageSync('token')
				})
				if (res.status === false) {
					const jg = uni.showModal({
						content: '请登录',
						confirmText: '确定',
						cancelText: '取消',
						success(data) {
							if (data.confirm) {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}
						}
					});
				}
			},

			// 获取首页数据
			async getHomeData() {
				const {
					data: res
				} = await getHomeDataApi({
					method: "pages.gethomepageconfig",
					token: this.token
				})
				// console.log(res.data.items);
				this.imgSlide = res.data.items[1].params.list;
				this.navBar = res.data.items[3].params.list;
				this.hotGoods = res.data.items[4].params.list;
				this.hotGoods.forEach(item => {
					if (item.name.length >= 26) {
						item.name = `${item.name.substring(0, 23)}...`
					}
				})
			},

			// 去详情页
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/goodsDetail/goodsDetail?id=' + id
				})
			},

			// 查看更多
			lookMore() {
				uni.navigateTo({
					url: '/pages/shopList/shopList?keyWord=' + ''
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.search {
		width: 90%;
		margin: 0 auto;
		height: 36px;
		background-color: #ececec;
		color: #000;
		padding: 0 20px 0 10px;
		border-radius: 100px;
		font-size: 13px;
		margin-top: 10px;
		display: flex;
		align-items: center;
		justify-content: space-between;

		image {
			width: 20px;
			height: 20px;
		}
	}

	.swiperOuter {
		width: 100%;
		height: 153px;
		margin-top: 10px;

		.swiper-item {
			width: 100%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.navigationBtn {
		width: 100%;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 20px;
		margin-top: 20px;

		li {
			width: 16%;
			font-size: 12px;
			text-align: center;

			image {
				width: 40px;
				height: 40px;
				border-radius: 50%;
			}
		}
	}

	.hotShop {
		margin-top: 20px;

		.hotShop_top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 20px;
			margin-bottom: 20px;

			li {
				font-size: 16px;

				&:last-child {
					font-size: 13px;
					color: #9f9f9f;
				}
			}
		}

		.hotCard {
			width: 100%;
			padding: 0 20px;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			flex-wrap: wrap;


			li {
				width: 46%;
				height: 200px;
				padding: 0 0 10px 0;
				margin-bottom: 20px;

				image {
					width: 100%;
					height: 80%;
				}

				p {
					font-size: 12px;

					&:last-child {
						color: red;
						margin-top: 6px;
					}
				}

			}
		}

	}

	.footerImg {
		width: 100%;
	}
</style>